/**
 * Created by chesw on 2017/3/31.
 */
//页面加载完成，显示整个页面
function showHtml() {
    Vue.nextTick(function () {
        var elem = document.getElementsByTagName('body');
        elem[0].style.display = 'block';
    })
}
//解析url参数
function paramUrl(p_url) {
    if (!p_url) return;

    var tempUrl = null, param = null, paramArr = {};
    tempUrl = p_url.split('?')[1];
    if (tempUrl) {
        param = tempUrl.split('&');
        for (var i = 0; i < param.length; i++) {
            var temp = param[i].split('=');
            paramArr[temp[0]] = temp[1];
        }
    }
    return paramArr;
}
function textOverflow() {
    $('.one-overflow').xdlines({
        max: 2  //设置文本的最大行数
    });
    $('.two-overflow').xdlines({
        max: 3  //设置文本的最大行数
    });
    $('.three-overflow').xdlines({
        max: 4  //设置文本的最大行数
    });
    $('.four-overflow').xdlines({
        max: 5  //设置文本的最大行数
    });
    $('.five-overflow').xdlines({
        max: 6  //设置文本的最大行数
    });
}
function overflowEllipsis(p_this) {
    p_this.$nextTick(function () {
        textOverflow();
    });
}

/* 提示框
 * p_obj: {url: 'logo', text:'提示文字'}
 * */
function dialogTip(p_obj) {
    var _html = null;
    var _doc = document.getElementsByClassName('html-alert');
    if (_doc.length <= 0) {
        _html = document.createElement('div');
        _html.className = 'html-alert';
        _html.innerHTML = '<figure class="tip_box">' +
            '<img src="' + p_obj.url + '">' +
            '<p class="title">' + p_obj.text + '</p>' +
            '</figure>';
        document.body.appendChild(_html);
    } else {
        _html = document.createElement('figure');
        _html.className = 'tip_box';
        _html.innerHTML = '<img src="' + p_obj.url + '">' +
            '<p class="title">' + p_obj.text + '</p>';
        _doc[0].appendChild(_html);
    }

    var _newDoc = document.getElementsByClassName('html-alert');
    if (p_obj.text.length <= 20) { //提示文字长度小于等于20则1秒后消失
        setTimeout(function () { _newDoc[0].parentNode.removeChild(_newDoc[0]); }, 1000);
    }
    else if (p_obj.text.length > 20) {//提示文字长度大于20则2秒后消失
        setTimeout(function () { _newDoc[0].parentNode.removeChild(_newDoc[0]); }, 2000);
    }
}

//生成和验证验证码
var code;
function createCode() 
{
    code = "";
    var codeLength = 4; //验证码的长度
    var checkCode = document.getElementById("checkCode");
    var codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
         'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //所有候选组成验证码的字符，当然也可以用中文的
    for(var i = 0; i < codeLength; i++) 
    {
        var charNum = Math.floor(Math.random() * 52);
        code += codeChars[charNum];
    }
    if(checkCode) 
    {
        checkCode.className = "code";
        checkCode.innerHTML = code;
    }
}
//处理显示注册和登录页面
function checkLoginType(param)
{
    var loginType = param;
    var loginTab = document.getElementById("loginTab");
    var registerTab = document.getElementById("registerTab");
    var login = document.getElementById("login");
    var register = document.getElementById("register");
    var tipBox = document.getElementById("tipBox");

    if(loginType == "reg")
    {
        loginTab.removeAttribute("class", "navActive");
        registerTab.setAttribute("class", "navActive");
        login.style.display = "none";
        registerSuccess.style.display = "none";
        register.style.display = "block";
        tipBox.style.display = "none";
    }
    else
    {
        loginTab.setAttribute("class", "navActive");
        registerTab.removeAttribute("class", "navActive");
        login.style.display = "block";
        register.style.display = "none";
        tipBox.style.display = "none";
    }
}

var clock = '';
var nums = 60;
var btn;
function sendCode(thisBtn) {
    btn = thisBtn;
    btn.disabled = true; //将按钮置为不可点击
    btn.innerHTML = nums + "s 后重新获取";
    clock = setInterval(doLoop, 1000); //一秒执行一次
}

function doLoop() {  
    nums--;
    if (nums > 0) {
        btn.innerHTML = nums + "s 后重新获取";
    } else {
        clearInterval(clock); //清除js定时器
        btn.disabled = false;
        btn.innerHTML = "获取验证码";
        nums = 60; //重置时间
    }
}

//验证码倒计时
function countDown(p_id) {
    var code = document.getElementById(p_id);
    var time = 60;
    code.disabled = true;
    code.innerText = time + 's 后重新获取'; //倒计时时间
    var timeSt = setInterval(function () {
        time = time - 1;
        code.innerText = time + 's 后重新获取';
        if (time == 0) {
            clearInterval(timeSt);
            code.class = '';
            code.innerText = '获取验证码';
            code.disabled = false;
        }
    }, 1000)
}

function checkValidateCode(_self)
{
    varRet = true;
    tipBox = document.getElementById("tipBox");
    _self.$http.post("/Login/GetCurrentValidateCode").then(function (ret) {
        if (ret.body.Code == 1) {
            currentValCode = ret.body.Msg;
            varRet = false;
        }
        else {
            tipBox.style.display = "block";
            _self.sLoginTip = ret.body.Msg;
            varRet = false;
        }
        return varRet;
    })   
}


function CheckLoginStatus(_self, loginInfor, p_goHome, p_getLoginInfo)
{
    if (p_getLoginInfo == null)
        p_getLoginInfo = false;
    if (p_goHome == null) 
        p_goHome = true;

    _self.$http.post("/Home/CheckLoginStatus").then(function (res) {
        if (res.body != null) {
            loginInfor.isLogin = res.body.isLogin;
            loginInfor.name = res.body.userName;
            loginInfor.mobile = res.body.mobile;
            loginInfor.picUrl = res.body.picUrl;

            if (!p_getLoginInfo) {
                var reg = /^(\d{3})\d{4}(\d{4})$/;
                if (loginInfor.mobile != null) {
                    loginInfor.mobile = loginInfor.mobile.replace(reg, "$1****$2");
                }

                if (loginInfor.name != null && loginInfor.name != "")
                    _self.userName = loginInfor.name;
                else {
                    _self.userName = loginInfor.mobile;
                }

                var RegistBar = document.getElementById("RegistBar");
                var LoginBar = document.getElementById("LoginBar");
                var QkResv = document.getElementById("QkResv");                               
                //显示指定的头菜单
                if (loginInfor.isLogin == true) {
                    RegistBar.style.display = "none";
                    LoginBar.style.display = "block";
                    QkResv.style.display = "none";
                    if (window.location.href.indexOf("ForgetPwd") != -1 && p_goHome)
                        window.location.href = "../Home/Index";

                    if (window.location.href.indexOf("PersonalCenter") != -1)
                        _self.CheckPersonInfo();
                }
                else {
                    RegistBar.style.display = "block";
                    LoginBar.style.display = "none";
                    QkResv.style.display = "block";

                    if (window.location.href.indexOf("PersonalCenter") != -1) {
                        window.location.href = "../Login/Index";
                    }
                }
            }          
        }
    });
}
//左边导航栏，切换颜色修改
function changeColorByLNav(p_obj, p_index1, p_index2) {
    for (var i = 0 ; i < p_obj.tabList.length; i++) {
        for (var j = 0; j < p_obj.tabList[i].list.length; j++) {
            if (i == p_index1 && j == p_index2) {
                p_obj.tabList[i].list[j].cls = GlobalValue.VALIDTAB;
            } else {
                p_obj.tabList[i].list[j].cls = GlobalValue.INVALIDTAB;
            }
        }
    }
    return p_obj;
}

function showPageRecords(p_curPage,p_Info) {
    var newPageInfo = [];
    if (p_curPage > 0) {

        if (p_curPage > p_Info.pageCount && p_Info.pageCount != 0) {
            p_curPage = p_Info.pageCount;
        }

        //判断数据是否需要更新
        var currentPageCount = Math.ceil(p_Info.totalCount / p_Info.pageSize);
        if (currentPageCount != p_Info.pageCount) {
            p_Info.pageCount = currentPageCount;
        }
        //else if (p_Info.pageCurrent == p_curPage && currentPageCount == p_Info.pageCount) {
        //    console.log("not refresh");
        //    return;
        //}

        //从所有数据中取分页数据       
        for (var i = 0; i < p_Info.pageSize; i++) {
            var index = i + (p_curPage - 1) * p_Info.pageSize;
            if (index > p_Info.totalCount - 1) break;
            newPageInfo[newPageInfo.length] = p_Info.allRecord[index];
        }       
    }
    p_Info.pageCurrent = p_curPage;
    p_Info.curPageRecord = newPageInfo;

    return p_Info;
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2], "utf-8");
    return null;
}

function showPdf(isShow) {
    var state = "";
    if (isShow) {
        state = "block";
    } else {
        state = "none";
    }
    var pop = document.getElementById("pop");
    pop.style.display = state;
    var lightbox = document.getElementById("lightbox");
    lightbox.style.display = state;
}
function close() {
    showPdf(false);
}

//function runSave() {
//    if (FileFrame.location != "about:blank") {
//        window.FileFrame.document.execCommand("SaveAs");
//    }
//} 
//判断参数是否为空
function isEmpty(p_val) {
    if (p_val == null || p_val == "") {
        return true;
    } else {
        return false;
    }
}
//验证用户名有效性
function isValidOfName(p_name, p_err) {
    if (isEmpty(p_name)) {
        p_err.type = 1;
        p_err.info = '请输入姓名';
        return false;
    } else if (!(/[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*/.test(p_name))) {
        p_err.type = 1;
        p_err.info = '请输入正确的姓名';
        return false;
    } else {
        p_err.type = -1;
        p_err.info = '';
        return true;
    }
}
//验证手机号码有效性
function isValidOfPhone(p_phone, p_err) {
    if (isEmpty(p_phone)) {
        p_err.type = 2;
        p_err.info = '请输入手机号码';
        return false;
    }else if (!(/^1[34578]\d{9}$/.test(p_phone))) {
        p_err.type = 2;
        p_err.info = '请输入正确格式的手机号码';
        return false;
    } else {
        p_err.type = -1;
        p_err.info = '';
        return true;
    }
}
//验证验证码有效性
function isValidOfCode(p_code, p_err) {
    if (isEmpty(p_code)) {
        p_err.type = 3;
        p_err.info = '请输入验证码';
        return false;
    } else {
        p_err.type = -1;
        p_err.info = '';
        return true;
    }
}
//验证证件号有效性
function isValidOfCard(p_card, p_err) {
    if (isEmpty(p_card)) {
        p_err.type = 4;
        p_err.info = '请输入证件号码';
        return false;
    } else if (!(/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/.test(p_card))) {
        p_err.type = 4;
        p_err.info = '请输入正确的证件号码';
        return false;
    } else {
        p_err.type = -1;
        p_err.info = '';
        return true;
    }
}

//验证证件号有效性
function isValidPassword(p_password, p_err) {
    if (isEmpty(p_password)) {
        p_err.info = '请输入密码';
        return false;
    } else if (!(/^[a-zA-Z\d]{8,20}$/.test(p_password))) {
        p_err.info = '请设置8~20位的字母或数字为密码';
        return false;
    } else {
        p_err.type = -1;
        p_err.info = '';
        return true;
    }
}

function addHandler(element, type, handler) {
    if (element.addEventListener) {
        element.addEventListener(type, handler, false);
    } else if (element.attachEvent) {
        element.attachEvent("on" + type, handler);
    } else {
        element["on" + type] = handler;
    }
}
